package com.zhou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhou.entity.Outbound;
import com.zhou.mapper.OutboundMapper;
import com.zhou.service.OutboundService;
import com.zhou.utis.booleanUtils.BooleanEaun;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class OutboundServiceImpl implements OutboundService {
    @Autowired
    private OutboundMapper outboundMapper;

    /**
     * 出库查询
     *
     * @param page     1
     * @param pageSize 10
     * @return
     */
    @Override
    public List<Outbound> getAll(Integer page, Integer pageSize) {
        LambdaQueryWrapper<Outbound> wrapper = new LambdaQueryWrapper<>();

        Page<Outbound> pg = new Page<>(page, pageSize);
        IPage<Outbound> outboundPage = outboundMapper.selectPage(pg, wrapper);
        if (outboundPage == null) {
            throw new RuntimeException("出库表分页查询失败");
        }
        List<Outbound> records = outboundPage.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            throw new RuntimeException("出库表内没有数据，请联系管理员");
        }
        return records;
    }

    /**
     * 删除出库记录
     *
     * @param id
     * @return
     */
    @Override
    public Boolean delete(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new RuntimeException("参数不能为空");
        }
        Outbound outbound = outboundMapper.selectById(id);
        if (StringUtils.isEmpty(outbound)) {
            throw new RuntimeException("没有查到数据，请重试");
        }
        outbound.setDelFlag(BooleanEaun.TURE.getResultValue());
        int i = outboundMapper.updateById(outbound);
        if (i == 0) {
            throw new RuntimeException("删除失败，请重试");
        }
        return true;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Outbound getById(String id) {
        if (StringUtils.isEmpty(id)){
            throw new RuntimeException("参数不能为空，请重试");
        }
        Outbound outbound = outboundMapper.selectById(id);
        if (StringUtils.isEmpty(outbound)||StringUtils.isEmpty(outbound.getDelFlag().equals(BooleanEaun.TURE.getResultValue()))){
            throw new RuntimeException("没有查到数据，请重试");
        }
        return outbound;
    }

}
